package com.hmily.dubbo.consumer.sync;

import com.hmily.dubbo.api.UserQueryFacade;
import com.hmily.dubbo.api.UserUpdateFacade;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.ReferenceConfigBase;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.utils.ReferenceConfigCache;

import java.io.IOException;

import static org.apache.dubbo.common.BaseServiceMetadata.buildServiceKey;

/**
 * <h1>利用 java 代码来启动消费方。</h1>
 *
 * @author hmilyylimh
 * ^_^
 * @version 0.0.1
 * ^_^
 * @date 2021-10-18
 */
public class CacheGeneraterConsumerApplication {

    public static void main(String[] args) throws IOException {
        // 设置应用服务名称
        ApplicationConfig applicationConfig = new ApplicationConfig("dubbo-consumer");
        // 设置注册中心的地址
        RegistryConfig registryConfig = new RegistryConfig("zookeeper://127.0.0.1:2181");
        CustomKeyGenerator customKeyGenerator = new CustomKeyGenerator();

        // 创建引用 UserQueryFacade 这个服务的对象
        ReferenceConfig<UserQueryFacade> referenceConfig = new ReferenceConfig<>();
        referenceConfig.setApplication(applicationConfig);
        referenceConfig.setRegistry(registryConfig);
        referenceConfig.setInterface(UserQueryFacade.class);

        ReferenceConfigCache cache = ReferenceConfigCache.getCache("_name", customKeyGenerator);
        UserQueryFacade userQueryFacade = cache.get(referenceConfig);

        // UserQueryFacade userQueryFacade = referenceConfig.get();
        System.out.println(userQueryFacade);
        System.out.println(userQueryFacade.queryUser("yuki"));

        ReferenceConfig<UserQueryFacade> referenceConfig2 = new ReferenceConfig<>();
        referenceConfig2.setApplication(applicationConfig);
        referenceConfig2.setRegistry(registryConfig);
        referenceConfig2.setInterface(UserQueryFacade.class);

        ReferenceConfigCache cache2 = ReferenceConfigCache.getCache("_name", customKeyGenerator);
        UserQueryFacade userQueryFacade2 = cache2.get(referenceConfig2);

        // UserQueryFacade userQueryFacade2 = referenceConfig2.get();
        System.out.println(userQueryFacade2);
        System.out.println(userQueryFacade2.queryUser("yuki"));

        // 阻塞等待
        System.in.read();
    }

    public static class CustomKeyGenerator implements ReferenceConfigCache.KeyGenerator{

        @Override
        public String generateKey(ReferenceConfigBase<?> referenceConfig) {
            String iName = referenceConfig.getInterface();
            if (StringUtils.isBlank(iName)) {
                Class<?> clazz = referenceConfig.getInterfaceClass();
                iName = clazz.getName();
            }
            if (StringUtils.isBlank(iName)) {
                throw new IllegalArgumentException("No interface info in ReferenceConfig" + referenceConfig);
            }

            return buildServiceKey(iName, referenceConfig.getGroup(), referenceConfig.getVersion());
        }
    }

    public static String buildServiceKey(String path, String group, String version) {
        StringBuilder buf = new StringBuilder();
        if (StringUtils.isNotEmpty(group)) {
            buf.append(group).append("/");
        }
        buf.append(path);
//        if (StringUtils.isNotEmpty(version)) {
//            buf.append(":").append(version);
//        }
        return buf.toString();
    }
}
